<?php
namespace app\admin\controller;

use app\common\controller\Admin;

class Route extends Admin
{
    //定义模块字段列表
    protected function fields($action='create', $data=[])
    {
        //合并初始数据
        $data = DcArrayArgs(config('form_data.admin_route_'.$action), $data);
        //所有表单字段预埋钩子配置
        return DcArrayArgs(config('form_fields.admin_route_'.$action), \daicuo\Route::fields($data));
    }
	
    //新加一条规则到数据库
	public function save()
    {
        config('common.validate_name', config('validate_name.admin_route_save')  );
        
        $op_id = \daicuo\Route::save(input('post.'));
        
		if($op_id < 1){
			$this->error(\daicuo\Route::getError());
		}
        
		$this->success(lang('success'));
	}
    
    //删除路由规则
	public function delete()
    {
        $ids = input('id/a');
		if( !$ids ){
			$this->error(lang('mustIn'));
		}
        
        foreach($ids as $id){
            \daicuo\Route::delete_id($id);
        }
        
        $this->success(lang('success'));
	}
    
    //修改
	public function edit()
    {
		if( !$id = input('id/d',0) ){
			$this->error(lang('mustIn'));
		}
        
		//查询数据
        $data = \daicuo\Route::get_id($id, false);
        if( is_null($data) ){
            $this->error(lang('empty'));
        }
        
        $this->assign('query', $this->query);
        
		$this->assign('data', $data);
        
        $this->assign('fields', $this->formFields('edit', $data));
        
		return $this->fetch();
	}
	
	//修改一条规则到数据库
	public function update()
    {
        $data = input('post.');
        if(!$data['op_id']){
			$this->error(lang('mustIn'));
		}
        
        config('common.validate_name', config('validate_name.admin_route_update') );
        
        $info = \daicuo\Route::update_id($data['op_id'], $data);
        if(is_null($info)){
            $this->error(\daicuo\Route::getError());
        }
    
        $this->success(lang('success'));
	}
    
    //路由管理
	public function index()
    {
        if( $this->request->isAjax() ){
            $args = array();
            $args['cache']         = false;
            $args['field']         = '*';
            //$args['limit']         = input('pageSize/d', 20);
            //$args['page']          = input('pageNumber/d', 1);
            $args['sort']          = input('get.sortName/s','op_id');
            $args['order']         = input('get.sortOrder/s','desc');
            $args['search']        = input('searchText/s','');
            $args['where']         = DcWhereQuery(['op_module','op_controll','op_action','op_status'], 'eq', $this->query);
            //数据返回
            if($infos = DcRouteSelect($args) ){
                return json($infos);
            }
            return json([]);
		}
        //地址栏参数
        $this->assign('query', $this->query);
        //表单筛选字段
        $this->assign('fields', $this->formFields('index', $this->query));
        //表格列字段
        $this->assign('columns', $this->tableColumns('index', $this->query));
        //加载模板
		return $this->fetch();
	}
    
    //排序（拖拽ajax）
	public function sort()
    {
		if( !\daicuo\Op::sort(input('get.id')) ){
            $this->error(lang('fail'));
        }
        
        DcCache('route_all', NULL);
        
        $this->success(lang('success'));
	}
    
    //预览
	public function preview()
    {
		if( !$id = input('id/d',0) ){
			$this->error(lang('mustIn'));
		}
        
        $data = \daicuo\Route::get_id($id, false);
        
        $this->redirect(DcUrlAdmin($data['address']), 302);
	}
}